// subcontract/Home/confirmOrder/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    defaultress: {},//默认地址
    orderInfo: {},
    settlementInfo: {},
    page: 1,//页码
    pagingState: false,//分页状态
    goodsList: [],//商品
  },
  async submitOrder() {
    let { orderInfo, defaultress } = this.data
    console.log(orderInfo);
    let object = {
      goods: orderInfo.goods,
      goods_type: '1',
      address_id: defaultress.id || 0,
      pay_way: '1',
      delivery_type: '0',
      cart_id: orderInfo.carts,
    }
    const { code, data } = await getApp().api.submitOrder(object)
    if (code == 1) {
      this.unifiedpay(data.type, data.trade_id)
    }
  },
  /**支付方式 */
  async unifiedpay(from, order_id) {
    let object = {
      order_id,
      from,
      pay_way: 1
    }
    const { code, data } = await getApp().api.unifiedpay(object)
    if (code == 1) {
      const ret = await getApp().requestPay(data)
      if (ret.errMsg == 'requestPayment:ok') {
        getApp().showToast('支付成功!')
        setTimeout(() => {
          wx.navigateTo({
            url: '/subcontract/Mine/order/index',
          })
        }, 2000)
      } else {
        getApp().showToast('支付失败！')
      }
    }
  },
  /**商品列表 */
  async getGoodsList() {
    let object = {
      page_no: this.data.page,
      is_rand: 1
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.getGoodsList(object)
      if (code == 1) {
        this.setData({
          goodsList: this.data.goodsList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  /**获取用户默认地址 */
  async getDefault() {
    const { code, data } = await getApp().api.getDefault()
    if (code == 1) {
      this.setData({
        defaultress: data
      })
      this.settlement()
    }
  },
  /**计算运费 */
  async settlement() {
    let { orderInfo, defaultress } = this.data
    console.log(orderInfo);
    let object = {
      goods: orderInfo.goods,
      cart_id: orderInfo.carts,
      address_id: defaultress.id || 0,
    }
    const { code, data } = await getApp().api.settlement(object)
    if (code == 1) {
      this.setData({
        settlementInfo: data
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getGoodsList()
    let TTUL = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('confirmOrder', function (data) {
      TTUL.setData({
        orderInfo: data
      })
      TTUL.getDefault()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getGoodsList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})